package animation.core;

import java.util.Map;
import java.util.HashMap;
import java.util.Collections;
import java.util.TreeMap;

/**
 * Created by IntelliJ IDEA.
 * User: Pablo
 * Date: Aug 9, 2008
 * Time: 4:36:34 AM
 * To change this template use File | Settings | File Templates.
 */
public class MonitorModelImpl implements AlgorithmMonitorModel{

    private String algorithmName;
    private Map<String, String> variables = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
    private Map<String, String> invariants = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
    private Map<String, Integer> counters = new TreeMap<String, Integer>(String.CASE_INSENSITIVE_ORDER);

    public MonitorModelImpl(String algorithmName) {
        this.algorithmName = algorithmName;
    }

    public String getAlgorithmName() {
        return algorithmName;
    }

    public void registerVariable(String name, String initialValue) {
        if (variables.containsKey(name)){
            throw new IllegalArgumentException("Variable with name '"+name+"' already exists");
        }

        variables.put(name, initialValue);
    }

    public void registerInvariant(String name, Object value) {
        registerInvariant(name, value.toString());
    }

    public void registerInvariant(String name, String value) {
        if (variables.containsKey(name)){
            throw new IllegalArgumentException("Invariant with name '"+name+"' already exists");
        }

        invariants.put(name, value);
    }

    public void setVariableValue(String name, String value) {
        if (!variables.containsKey(name)){
            throw new IllegalArgumentException("There is no registered variable with name '"+name+"'");
        }

        variables.put(name, value);
    }

    public void setVariableValue(String name, Object value) {
        setVariableValue(name, value.toString());
    }

    public void registerCounter(String name, int initialValue) {
        if (counters.containsKey(name)){
            throw new IllegalArgumentException("Counter with name '"+name+"' already exists");
        }

        counters.put(name, initialValue);
    }

    public void incrementCounter(String name) {
        if (!counters.containsKey(name)){
            throw new IllegalArgumentException("There is no registered counter with name '"+name+"'");
        }

        counters.put(name, counters.get(name) + 1);
    }

    public Map<String, Integer> getCounters() {
        return Collections.unmodifiableMap(counters);
    }

    public Map<String, String> getVariables() {
        return Collections.unmodifiableMap(variables);
    }

    public Map<String, String> getInvariants() {
        return Collections.unmodifiableMap(invariants);
    }
}
